package com.golden.procedure.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.procedure.client.EmployeeClientAPI;
import com.golden.procedure.clientDO.EmployeeDO;
import com.golden.procedure.dao.WorkCommentMapper;
import com.golden.procedure.domain.WorkCommentDO;
import com.golden.procedure.entity.WorkComment;
import com.golden.procedure.entity.WorkCommentExample;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.ResultSupport;
import com.golden.procedure.service.IWorkCommentService;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;

@Service("workCommentService")
public class WorkCommentService extends BaseService implements IWorkCommentService {

	@Autowired
	EmployeeClientAPI employeeClientAPI;
	@Autowired
	WorkCommentMapper workCommentMapper;

	/**
	 * 添加工单执行备注 <b>Description: </b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月17日 下午5:47:17 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO add(WorkCommentDO domain) {
		// 添加工单执行备注
		ResultDO result = new ResultSupport();
		WorkComment entity = new WorkComment();
		if (!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("数据转换异常");
			return result;
		}

		// TODO: 查询执行人所在部门
		EmployeeDO employeeDO = getEmployee(domain.getAuthorID());
		entity.setOrgID(employeeDO.getOrgId());
		entity.setOrgName(employeeDO.getDepartment());
		entity.setIsDelete(BonConstant.DB_NORMAL);
		entity.setCreatetime(Calendar.getInstance().getTime());

		int ret = workCommentMapper.insert(entity);
		if (ret < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("工单执行备注记录添加失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	/**
	 * 查询工单服务记录 <b>Description: 通过工单id查询服务记录</b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月17日 下午5:47:17 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO queryByWorkOrderID(Integer workOrderId) {
		// 添加工单执行备注
		ResultDO result = new ResultSupport();
		if (isNumberInValid(workOrderId)) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			return result;
		}

		WorkCommentExample example = new WorkCommentExample();
		WorkCommentExample.Criteria criteria = example.createCriteria();
		criteria.andWorkOrderIDEqualTo(workOrderId);
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);
		List<WorkComment> list = workCommentMapper.selectByExample(example);

		result.setModel(ResultDO.FIRST_MODEL_KEY, getWorkCommentDOList(list));
		return result;
	}

}
